package com.meiqiongproperty.yiguanjia.fragment.home.tenement;

import android.app.Dialog;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.meiqiongproperty.yiguanjia.R;
import com.meiqiongproperty.yiguanjia.databinding.GasfeeLayoutBinding;
import com.meiqiongproperty.yiguanjia.db.utils.DBUserUtils;
import com.meiqiongproperty.yiguanjia.fragment.BaseFragment;
import com.meiqiongproperty.yiguanjia.fragment.pay.PayTypeFragment;
import com.meiqiongproperty.yiguanjia.http.api.ApiService;
import com.meiqiongproperty.yiguanjia.http.interfaces.SimpleHttpCallback;
import com.meiqiongproperty.yiguanjia.http.sendJsonModel.SendLifePay;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.WaterWX;
import com.meiqiongproperty.yiguanjia.httpbean.tenement.pay.WaterZFB;
import com.meiqiongproperty.yiguanjia.ui.dialog.FragmentExchangeController;
import com.meiqiongproperty.yiguanjia.ui.picker.DatePickerDialog;
import com.meiqiongproperty.yiguanjia.ui.picker.DateUtil;
import com.meiqiongproperty.yiguanjia.utils.Utils;

import java.util.List;

/**
 * @version V1.0
 * @packge com.meiqiongproperty.yiguanjia.fragment.home.tenement
 * @User ChenJing
 * @Date 2017/5/29
 */

public class GasFeeFragment extends BaseFragment{
    GasfeeLayoutBinding binding;
    Dialog dateDialog;
    String time, jfhh, zdh, fy;
    int type;
    int[] date;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.gasfee_layout, container, false);
        binding.setClick(this);
        binding.navigation.setTitle("燃气费");
        binding.navigation.setLeftClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendKeyBackEvent();
            }
        });
        return binding.getRoot();
    }

    public void commitPay(View view) {
        if (CheckValue()) {
            SendChargePropertyMethod();
        }

    }

    private void SendChargePropertyMethod() {
        //缴费类型,3"水费",4"电费",5"固话宽带",6"有线电视",7"燃气费"
        SendLifePay sendChargeProperty = new SendLifePay();
        sendChargeProperty.setType(7);
        sendChargeProperty.setPayMethod(type);
        sendChargeProperty.setToken(DBUserUtils.getUserInfo(getActivity()).getToken());
        sendChargeProperty.setRoomID(DBUserUtils.getmDefaultHouse(getActivity()).getID());
        sendChargeProperty.setBillNo(zdh);//账单号
        sendChargeProperty.setRoomNo(jfhh); //户号
        sendChargeProperty.setBillPrice(fy);//费用
        sendChargeProperty.setMonth(time);
        switch (type) {
            case 1:
                ApiService.chargeLife(getActivity(), new SimpleHttpCallback<WaterZFB>() {
                    @Override
                    public void onNext(WaterZFB commPayZFB) {
                        String orderInfo = commPayZFB.getParameter().getParameter();   // 订单信息
                        doAlipay(orderInfo);
                    }
                }, Utils.getRequestBody(sendChargeProperty));
                break;
            case 2:
                ApiService.chargeLifeWX(getActivity(), new SimpleHttpCallback<WaterWX>() {
                    @Override
                    public void onNext(WaterWX commPayWX) {
                        doWaterOrderWXPay(commPayWX.getParameter().getParameter(),commPayWX.getParameter().getParameter().getAppid());
                    }
                }, Utils.getRequestBody(sendChargeProperty));
                break;
        }

    }

    private boolean CheckValue() {
        time = binding.time.getText().toString();
        jfhh = binding.jfhh.getEditableText().toString();
        zdh = binding.zdh.getEditableText().toString();
        fy = binding.fy.getEditableText().toString();
        if (TextUtils.isEmpty(time)) {
            Utils.showDialog(getActivity(), "请选择时间");
            return false;
        }
        if (TextUtils.isEmpty(jfhh) && TextUtils.isEmpty(zdh)) {
            Utils.showDialog(getActivity(), "缴费账户和账单号必须填一个");
            return false;
        }
        if (TextUtils.isEmpty(fy)) {
            Utils.showDialog(getActivity(), "请输入缴费金额");
            return false;
        }
        return true;
    }

    public void selectorDate(View view) {
        showDateDialog(DateUtil.getDateForString(Utils.getYunyingData()));
    }

    public void select_pay_type(View view) {
        PayTypeFragment payTypeSeleor = new PayTypeFragment();
        payTypeSeleor.setPayTypeClickListener(new PayTypeFragment.PayTypeClickListener() {
            @Override
            public void setData(Object data) {
                type = (int) data;
                switch (type) {
                    case 1:
                        binding.type.setText("支付宝支付");
                        break;
                    case 2:
                        binding.type.setText("微信支付");
                        break;
                }
            }
        });
        FragmentExchangeController.addFragment(getFragmentManager(), payTypeSeleor, payTypeSeleor.getTagName());
    }

    /**
     * 弹出时间选择器
     *
     * @param date
     */
    private void showDateDialog(List<Integer> date) {
        DatePickerDialog.Builder builder = new DatePickerDialog.Builder(getActivity());
        builder.setOnDateSelectedListener(new DatePickerDialog.OnDateSelectedListener() {
            @Override
            public void onDateSelected(int[] dates) {
                binding.time.setText(dates[0] + "年" + (dates[1] > 9 ? dates[1] : ( dates[1]))+"月");
            }

            @Override
            public void onCancel() {

            }
        })
                .setSelectYear(date.get(0) - 1)
                .setSelectMonth(date.get(1) - 1)
                .setSelectDay(date.get(2) - 1);

        builder.setMaxYear(DateUtil.getYear());
        builder.setMaxMonth(DateUtil.getDateForString(DateUtil.getToday()).get(1));
        builder.setMaxDay(DateUtil.getDateForString(DateUtil.getToday()).get(2));
        dateDialog = builder.create();
        dateDialog.show();
    }
}
